package com.yindian.cos.easy.security.authentication.login;

import com.yindian.cos.easy.security.exception.AccountUnavailableException;
import com.yindian.cos.easy.security.exception.PasswordErrorException;

import java.io.Serializable;

/**
 * 用户详细信息的抽象
 *
 * @author zhangchuanhao
 * @create 2018/11/18
 */
public abstract class UserDetail implements Serializable {

    /**
     * 用户在业务系统的唯一标识，用于jwt生成token时放入
     *
     * @return
     */
    public abstract String getUserId();

    /**
     * 需要自己实现密码匹配，因为加密的方式太多
     *
     * @return
     */
    public abstract boolean match();

    /**
     * 账号是否过期
     *
     * @return
     */
    public abstract boolean isAccountNonExpired();

    /**
     * 账号是否锁定
     *
     * @return
     */
    public abstract boolean isAccountNonLocked();

    /**
     * 凭证是否过期
     *
     * @return
     */
    public abstract boolean isCredentialsNonExpired();

    /**
     * 账号是否可用
     *
     * @return
     */
    public abstract boolean isEnabled();

    /**
     * 校验该用户是否可用
     */
    public void verify() {
        if (!match()) {
            throw new PasswordErrorException("密码错误，user_id = " + getUserId());
        }
        if (isAccountNonExpired()) {
            throw new AccountUnavailableException("账号已过期,user_id = " + getUserId());
        }
        if (isAccountNonLocked()) {
            throw new AccountUnavailableException("账号已锁定,user_id = " + getUserId());
        }
        if (isCredentialsNonExpired()) {
            throw new AccountUnavailableException("凭证已过期,user_id = " + getUserId());
        }
        if (!isEnabled()) {
            throw new AccountUnavailableException("账号不可用,user_id = " + getUserId());
        }
    }
}
